/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.config;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.LoggerContextListener;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.spi.ContextAwareBase;
import ch.qos.logback.core.spi.LifeCycle;
import org.openislands.oi.constant.Dict;
import org.openislands.oi.util.YamlUtils;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

/***
 * logback starting configuration
 */
public class LogbackContextListener extends ContextAwareBase implements LoggerContextListener, LifeCycle {
    @Override
    public void start() {
        final Properties properties = System.getProperties();
        final String property = properties.getProperty("sun.java.command");
        String serviceName = "bootstrap"; // , logPath = "logs"
        Object logLevel = null;
        Context context = getContext();
        try {
            if (property != null) {
                String[] serviceMark = {Dict.APISERVER_MARK, Dict.MASTER_MARK, Dict.WORKER_MARK, Dict.CONSOLE_MARK};
                String[] serviceClassName = Dict.getServiceClassNames();
                for (int i = 0; i < serviceMark.length; i++) {
                    if (property.contains(serviceClassName[i])) {
                        final Properties loadYamlSource = YamlUtils.loadYamlProperties(Dict.COMMON_CONFIG_FILE);
                        final Object masterConfig = loadYamlSource.get(serviceMark[i]);
                        if (masterConfig instanceof Map) {
                            final Map<?, ?> config = (Map<?, ?>) masterConfig;
                            logLevel = config.get(Dict.LOG_LEVEL_PROPERTY);
                        }
                        serviceName = serviceMark[i];
                        break;
                    }
                }
            }
        } catch (IOException ignored) {
        }
        properties.put(Dict.SERVER_NAME, serviceName);
        context.putProperty(Dict.SERVER_NAME, serviceName);
        context.putProperty(Dict.LOG_PATH_PROPERTY, Dict.LOG_PATH_ROOT);
        context.putProperty(Dict.LOG_LEVEL_PROPERTY, Objects.nonNull(logLevel) ? logLevel.toString() : Dict.LOG_LEVEL_DEFAULT);
    }

    @Override
    public void stop() {
    }

    @Override
    public boolean isStarted() {
        return false;
    }

    @Override
    public boolean isResetResistant() {
        return false;
    }

    @Override
    public void onStart(LoggerContext context) {
    }

    @Override
    public void onReset(LoggerContext context) {
    }

    @Override
    public void onStop(LoggerContext context) {
    }

    @Override
    public void onLevelChange(Logger logger, Level level) {
    }
}